<?php
/**
 * 站点设置控制器
 */
namespace Admin\Controller;
use Think\Controller;
class OptionController extends InitController {
   
	// 站点设置
	public function siteOpt() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$optionModel = D('Options');
		$opt = $optionModel->getOpts(array('type'=>1));

		$this->assign('opt',$opt);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Option/siteOpt');
	}
	
	// 站点设置
	public function setSiteOpt() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$post = I();

		if (empty($post)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		}

		$optionModel = M('Options');

		if($post['probation'] > 14){
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非会员试用期设置最大可设置14天','field'=>''));	
		}

		foreach ($post as $k=>$v) {
			$option_select = $optionModel->where(array('option_name'=>$k))->find();

			if(!empty($option_select)){
				$optionModel->where(array('option_name'=>$k))->save(array('option_value'=>$v));
			}else{
				$optionModel->add(array('option_value'=>$v,'option_name'=>$k));
			}
			
		}
		$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
	}
	
// 	// 首页设置
// 	public function indexSet() {

// 	}
	
	//微信支付设置
	public function wx_pay(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$optionModel = D('Options');
		$opt = $optionModel->getOpts();
		if(!empty($opt['wx_pay'])){
			$opt['wx_pay'] = unserialize($opt['wx_pay']);
		}
		if(!empty($opt['weixin_array'])){
			$opt['weixin_array'] = unserialize($opt['weixin_array']);
		}
		
		$this->assign('opt',$opt);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Option/wx_pay');
	}
	
	//微信支付设置操作
	public function do_wx_pay(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$post = I();
		if (empty($post)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		}

		$optionModel = M('Options');
		$optionModel->where(array('option_name'=>'wx_pay'))->save(array('option_value'=>serialize($post)));
		
		$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
	}
	
	
	//支付宝支付设置
	/* public function ali_pay(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$optionModel = D('Options');
		$opt = $optionModel->getOpts();

		if(!empty($opt['alipay'])){
			$opt['alipay'] = unserialize($opt['alipay']);
		}
		
		$this->assign('opt',$opt);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Option/ali_pay');
	} */
	
	//支付宝支付设置
	/* public function do_ali_pay(){
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$post = I();
		if (empty($post)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		}

		$optionModel = M('Options');
		$optionModel->where(array('option_name'=>'alipay'))->save(array('option_value'=>serialize($post)));
		
		$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
	} */
	
	
	// 会员等级列表
	public function levelList()
	{
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$level_list = getMemberLevelList();

		$this->assign('level_list',$level_list);

		$this->assign('opt',$opt);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Option/levelList');
	}

	// 会员等级编辑页
	public function levelEdit()
	{
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$level_id = I('level_id',0,'intval');
		
		// 获取等级列表
		$level_list = getMemberLevelList(false,$level_id);
		$level_info = $level_list[$level_id];
		
		$this->assign('level_info',$level_info);

		$this->assign('opt',$opt);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Option/levelEdit');
	}

	// 会员等级信息修改
	public function changeLevelInfo()
	{
		// 获取等级列表
		$level_id = I('level_id',0,'intval');
		$level_list = getMemberLevelList(false,$level_id);
		$level_info = $level_list[$level_id];
		

		$level_name = I('level_name','','strip_tags,htmlspecialchars,trim');
		$price = sprintf('%.2f',I('price',0,'floatval'));
		$valid_time = I('valid_time',0,'intval');
		$note = I('note','','strip_tags,htmlspecialchars,trim');

		$data = array();

		if (!empty($level_name))
		{
			$data['level_name'] = $level_name;
		}

		if (!empty($price))
		{
			$data['price'] = $price;
		}

		if (is_int($valid_time) && $valid_time >= 0)
		{
			$data['valid_time'] = $valid_time;
		}

		if (!empty($note))
		{
			$data['note'] = $note;
		}

		if (!empty($data))
		{
			
			if($level_info){
				$ac = M('level')->where(array('level_id' => $level_id))->save($data);
			}else{
				$ac = M('level')->add($data);
			}

			if ($ac === false)
			{
				$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
			}
			else
			{
				$this->ajaxReturn(array('error'=>0,'errmsg'=>'会员等级配置修改成功！','field'=>''));
			}
		}
	}
	
	//公告设置
	function notice(){
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		//查询所有配置
		$optionModel = D('Options');
		$opt = $optionModel->getOpts();
		$result = unserialize($opt['notice']);
		
		
		$this->assign('result',$result);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Option/notice');
		
	}
	
	
	//公告设置异步
	function ajax_notice(){
		//异步判断
		if(!IS_AJAX){
			return false;
		}
		
		//南瓜首页顶部图片
		// $banner_picture = I('banner_picture','');
		//首页中部图片
		$banner_picture_two = I('banner_picture_two','');
		//文章展示底部图片
		$publish_picture    = I('publish_picture','');
		
		//公告一
		$notice_one     = I('notice_one','','trim');
		//公告二
		$notice_two     = I('notice_two','','trim');
		//公告三
		$notice_three   = I('notice_three','','trim');
		
		//$url            = I('banner_picture_url','','trim');
		
		//判断是否是正确的url链接
		// if(!empty($url)){
		// 	if(!isUrl($url)){
		// 		$this->ajaxReturn(array('msg'=>'请输入正确的链接','error'=>'1'));
		// 	}
			
		// 	//拼装链接
		// 	if(strpos($url,'http') === false){
		// 		$url = 'http://'.$url; 
		// 	}
		// }
		
		$result = M('Options')->where(array('option_name'=>'notice'))->find();
		$data = array(
			// 'banner_picture'=>$banner_picture,
			'banner_picture_two'=>$banner_picture_two,
			'publish_picture'=>$publish_picture,
			'notice_one'=>$notice_one,
			'notice_two'=>$notice_two,
			'notice_three'=>$notice_three,
			// 'banner_picture_url'=>$url
		);
		
		$data = serialize($data);
		//序列化
		if(empty($result)){
			$judge = M('Options')->add(array('option_name'=>'notice','option_value'=>$data));
		}else{
			$judge = M('Options')->where(array('option_name'=>'notice'))->save(array('option_value'=>$data));
		}
		
		if($judge !== false){
			$this->ajaxReturn(array('msg'=>'保存成功','error'=>'0'));
		}else{
			$this->ajaxReturn(array('msg'=>'保存失败','error'=>'1'));
		}
		
		
	}
	
	//邮箱设置
	function mail(){
		//接受邮箱
		if(IS_AJAX){
			$mail = I('mail','','trim');
			if(!empty($mail)){
				
				$mail = str_replace('；',';',$mail);
				
				$mail_array = explode(';',$mail);
				if(!empty($mail_array)){
					foreach($mail_array as $k=>$v){
						if(!isEmail($v) && !empty($v)){
							$this->ajaxReturn(array('error'=>'1','errmsg'=>'邮箱格式错误'));
						}
					}
				}
				
				//设置邮箱
				$result = M('Options')->where(array('option_name'=>'send_mail'))->find();
				if(!empty($result)){
					$judge = M('Options')->where(array('option_name'=>'send_mail'))->save(array('option_value'=>$mail));
				}else{
					$judge = M('Options')->add(array('option_name'=>'send_mail','option_value'=>$mail));
				}
				
				if($judge !== false){
					$this->ajaxReturn(array('error'=>'0','errmsg'=>'保存成功'));
				}else{
					$this->ajaxReturn(array('error'=>'1','errmsg'=>'保存失败'));
				}
			}
			
		}
	
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		//查询所有配置
		$optionModel = D('Options');
		$opt = $optionModel->getOpts();
		$mail  = $opt['send_mail'];
		
		
		$this->assign('mail',$mail);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Option/mail');
		
	}
	
	
	
}